// import {
// 	API_URL
// } from '@/env'

export default {
	/**
	 * 图片处理-预览图片
	 * @param {Array} urls - 图片列表
	 * @param {Number} current - 首个预览下标
	 */
	previewImage(urls = [], current = 0) {
		uni.previewImage({
			urls: urls,
			current: current,
			indicator: 'default',
			loop: true,
			fail(err) {
				console.log('previewImage出错', urls, err)
			},
		})
	},

	/**
	 * 数据分组
	 * @param {Array} oArr - 原数组列表
	 * @param {Number} length - 单个数组长度
	 * @return {Array}  arr - 分组后的新数组
	 */
	splitData(oArr = [], length = 1) {
		let arr = [];
		let minArr = [];
		oArr.forEach(c => {
			if (minArr.length === length) {
				minArr = [];
			}
			if (minArr.length === 0) {
				arr.push(minArr);
			}
			minArr.push(c);
		});

		return arr;
	},

	/**
	 * @param {Object} feeArr 支付方式
	 */
	returnMoney(val) {
		let fee = val || []
		if (!fee.length) {
			return
		}
		let str = {
			price: 0,
			money: 0,
		};
		if (fee[0].status == 1) {
			str.price += parseInt(fee[0].price)
			return {
				txt: str.price + '元',
				type: 1
			}
		}
		if (fee[1].status == 1) {
			str.price += parseInt(fee[1].price)
			str.money += parseInt(fee[1].money)
			return {
				txt: str.price + '元' + '/裕米:' + str.money,
				type: 2
			}
		}
		if (fee[2].status == 1) {
			str.money += parseInt(fee[2].money)
			return {
				txt: '裕米:' + str.money,
				type: 3
			}
		}
		return {
			txt: ' 免费',
			type: 4
		}
		// if (str.price == 0) {
		// 	return '裕米:' + str.money
		// }
		// if (str.money == 0) {
		// 	return '' + str.price + '元'
		// }
		// if (str.price > 0 && str.money > 0) {
		// 	// return '现金:' + str.price + '元' + '/裕米:' + str.money
		// 	return '' + str.price + '元' + '/裕米:' + str.money
		// }
	},

	/**
	 * 剩余时间格式化
	 * @param {Number} t - 剩余多少秒
	 * @return {Object}  format - 格式后的天时分秒对象
	 */
	format(t) {
		let format = {
			d: '00',
			h: '00',
			m: '00',
			s: '00'
		};
		if (t > 0) {
			let d = Math.floor(t / 86400);
			let h = Math.floor((t / 3600) % 24);
			let m = Math.floor((t / 60) % 60);
			let s = Math.floor(t % 60);
			format.d = d < 10 ? '0' + d : d;
			format.h = h < 10 ? '0' + h : h;
			format.m = m < 10 ? '0' + m : m;
			format.s = s < 10 ? '0' + s : s;
		}
		return format;
	},


	calculateDiffTime(time) {
		let nowTime = Math.round(new Date() / 1000)
		let runTime = parseInt((time - nowTime));
		var year = Math.floor(runTime / 86400 / 365);

		runTime = runTime % (86400 * 365);

		var month = Math.floor(runTime / 86400 / 30);

		runTime = runTime % (86400 * 30);

		var day = Math.floor(runTime / 86400);

		runTime = runTime % 86400;

		var hour = Math.floor(runTime / 3600);

		runTime = runTime % 3600;

		var minute = Math.floor(runTime / 60);

		runTime = runTime % 60;

		var second = runTime;

		let obj = {
			year,
			month,
			day,
			hour,
			minute,
			second
		}
		return obj;
	},
	/**
	 * 计算相差的分钟
	 */
	setMinute(d1, d2) {
		// var d1 = new Date(timestr);//已知时间戳
		// var d2 = new Date();//当前时间戳
		let minute = (parseInt(d2 - d1) / 1000 / 60) //算出相差的分钟
		return minute
	},

	/**
	 * 打电话
	 * @param {String<Number>} phoneNumber - 数字字符串
	 */

	callPhone(phoneNumber = '') {
		let num = phoneNumber.toString()
		uni.makePhoneCall({
			phoneNumber: num,
			fail(err) {
				console.log('makePhoneCall出错', err)
			},
		});
	},
	//弹窗提示
	showMsg(msg) {
		uni.showToast({
			title: msg,
			icon: 'none',
			duration: 1500
		});
	},
	/**
	 * 返回上一页
	 */
	pagesBack() {
		let pages = getCurrentPages();
		if (pages.length > 1) {
			uni.navigateBack({ //返回
				delta: 1
			});
		}
		if (pages.length == 1) {
			uni.reLaunch({
				url: '/pages/index/index',
			})
		}
	},
	/**
	 * 返回上一页并提示
	 */
	pagesBackToast() {
		this.showMsg('请登录')
		setTimeout(res => {
			uni.navigateBack({
				delta: 1
			})
		}, 600)
	},
	//判断字符串是否为空 
	isEmptyOrSpaces(str) {
		return str === undefined || str === null || str.match(/^ *$/) !== null;
	},
	/**小程序图片转base64
	 * @param {Object} tempFilePath 图片临时路径
	 */
	imgUrlToBase64(tempFilePath) {
		return new Promise((resolve, rejects) => {
			uni.getFileSystemManager().readFile({
				filePath: tempFilePath, //选择图片返回的相对路径
				encoding: 'base64', //编码格式
				success: res => { //成功的回调
					let url = 'data:image/jpeg;base64,' + res.data
					resolve(url)
				},
				fail: (e) => {
					rejects(false)
					console.log("tools imgUrlToBase64 图片转换失败");
				}
			})
		})
	},
	/**
	 * 清空token
	 */
	logOut() {
		uni.setStorageSync("token", '')
	},
	/**
	 * 获取token
	 */
	getToken() {
		return uni.getStorageSync('token') || ''
	},
	/**
	 * 是否授权手机号
	 */
	isBindMible() {
		let userInfo = uni.getStorageSync('userInfo')
		return userInfo.mobile
	},
	getUserInfo() {
		let userInfo = uni.getStorageSync('userInfo')
		return userInfo
	},
	//是否登录
	isLogin() {
		let token = uni.getStorageSync('token')
		if (token) {
			return true
		} else {
			return false
		}
	},
	//时间戳转换年月日方法
	//format为“yyyy-mm-dd”、“yyyy/mm/dd”
	//str传时间戳
	//例： getUnixToDate('yyyy-mm-dd',"1618934400000")
	//返回："2021-04-21"
	getUnixToDate(format, str) {
		let oTime;
		var oDate = new Date(parseInt(str)),
			oYear = oDate.getFullYear(),
			oMonth = oDate.getMonth() + 1,
			oDay = oDate.getDate(),
			oHour = oDate.getHours(),
			oMin = oDate.getMinutes(),
			oSec = oDate.getSeconds();
		if (format == 'yyyy-mm-dd') {
			oTime = oYear + '-' + this.getzf(oMonth) + '-' + this.getzf(oDay) + ' ' + this.getzf(oHour) + ':' + this
				.getzf(oMin) + ':' + this.getzf(oSec); //最后拼接时间
		}
		return oTime.substring(0, 10);
	},
	//补0操作  
	getzf(num) {
		if (parseInt(num) < 10) {
			num = '0' + num;
		}
		return num;
	},
	getString(t) {
		let m = ''
		if (t > 0) {
			if (t < 10) {
				m = '0' + t
			} else {
				m = t + "";
			}
		} else {
			m = "00";
		}
		return m;
	},
	formatFun(t) {
		if (t < 60000) {
			let time = (t % 60000) / 1000
			console.log("time: ", time);
			return time.toFixed() + "秒";
		} else if ((t >= 60000) && (t < 3600000)) {
			return getString((t % 3600000) / 60000) + ":" + getString((t % 60000) / 1000);
		} else {
			return getString(t / 3600000) + ":" + getString((t % 3600000) / 60000) + ":" + getString((t % 60000) /
				1000);
		}
	},

}